// MicroDlg.cpp : implementation file
//

#include "stdafx.h"
#include "De-RSI.h"
#include "MicroDlg.h"


// CMicroDlg dialog

IMPLEMENT_DYNAMIC(CMicroDlg, CDialog)

CMicroDlg::CMicroDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMicroDlg::IDD, pParent)
{
	m_hIconHappy = AfxGetApp()->LoadIcon(IDI_HAPPY);
	m_hIconSad = AfxGetApp()->LoadIcon(IDI_SAD);
}

CMicroDlg::~CMicroDlg()
{
}

void CMicroDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_MICRO_ICON, m_cIconHappy);
	DDX_Control(pDX, IDC_PRG_FADE, m_ProgressFade);
}

void CMicroDlg::SetParameter(int work, int TmBeak)
{
    m_iWorkedTime = work;
    m_iBreakTime = TmBeak;
}

BOOL CMicroDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 
	CString strDisplay;
	strDisplay.Format(_T("You had worked incessantly for %d minutes, let your elbow and wrist have a rest!"), (30+m_iWorkedTime)/60);
	GetDlgItem(IDC_ST_MSG)->SetWindowText(strDisplay);
	
	m_iOneSecondTimer = SetTimer(5, TIMER_1S_CLOCK, 0);
	m_iMouseMoveCheckTimer = SetTimer(6, TIMER_MOUSE_CHECK_TM, 0);
	m_iPassSecond = 0;

	m_iResolutionX =  GetSystemMetrics(SM_CXSCREEN);  
	m_iResolutionY =  GetSystemMetrics(SM_CYSCREEN);
	GetWindowRect(&m_cDlgRc);
	m_iDlgWidth = m_cDlgRc.right - m_cDlgRc.left;
	m_idlgHeight = m_cDlgRc.bottom - m_cDlgRc.top;
	m_iCurX = (m_iResolutionX-m_iDlgWidth)/2;
	m_iCurY = (m_iResolutionY-m_idlgHeight)/2;

	SetWindowPos(&wndTopMost, m_iCurY, m_iCurY, 0,0, SWP_SHOWWINDOW|SWP_NOSIZE);
	m_cIconHappy.SetIcon(m_hIconHappy);
	m_iHappyDelay = 0;
	m_ProgressFade.SetRange(0,m_iBreakTime);
	m_ProgressFade.SetShowPercent(0);
	m_ProgressFade.AlignText(DT_CENTER);
	m_ProgressFade.SetBarColor(RGB(0,128,128));
	m_ProgressFade.SetTextColor(RGB(0,0,0));

	return TRUE;
}

#ifdef _DEBUG
VOID dbgPrintf(LPTSTR fmt, ...);
#endif


BOOL CMicroDlg::MouseInDlg()
{
	//dbgPrintf(_T("MouseinDlg %d %d %d -- %d %d %d"), m_cMousePosition.x, m_iDlgWidth, m_iCurX, m_cMousePosition.y, m_idlgHeight,m_iCurY );
	if (m_cMousePosition.x < m_iCurX)
	{
		//dbgPrintf(_T("False 1"));
		return FALSE;
	}
	if (m_cMousePosition.x > (m_iCurX + m_iDlgWidth ))
	{
		//dbgPrintf(_T("False 2"));
		return FALSE;
	}
	if (m_cMousePosition.y < m_iCurY)
	{
		//dbgPrintf(_T("False 3"));
		return FALSE;
	}
	if (m_cMousePosition.y > (m_iCurY + m_idlgHeight ))
	{
		//dbgPrintf(_T("False 4"));
		return FALSE;
	}
	//dbgPrintf(_T("!!TRUE!!"));
	return TRUE;
}

void CMicroDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnTimer(nIDEvent);

	if (m_iOneSecondTimer == nIDEvent)
	{
		if (++m_iPassSecond > m_iBreakTime)
		{
			CDialog::OnCancel();
		}
		CString strDisplay;
		strDisplay.Format(_T("%d"),m_iBreakTime-m_iPassSecond);
		//m_ProgressFade.SetTextColor(RGB(0,0,0));
		m_ProgressFade.SetWindowText(strDisplay);
		m_ProgressFade.SetPos(m_iPassSecond);
		//m_ProgressFade.RedrawWindow();
	}
	else if (m_iMouseMoveCheckTimer == nIDEvent)
	{
		GetCursorPos(&m_cMousePosition);
		if (FALSE == MouseInDlg())
		{
			if (++ m_iHappyDelay > HAPPY_DELAY_COUNT)
			{
				m_cIconHappy.SetIcon(m_hIconHappy);
				m_iHappyDelay = 0 - HAPPY_DELAY_COUNT;
			}
			return;
		}
		// mouse in middle of dialog
		srand( (unsigned)time( NULL ) );
		for (;;)
		{
			m_iCurX = rand() % (m_iResolutionX-m_iDlgWidth);
			m_iCurY = rand() % (m_iResolutionY-m_idlgHeight);
			if (FALSE == MouseInDlg())
			{
				break;
			}
		}
		SetWindowPos(&wndTopMost, m_iCurX, m_iCurY, 0,0, SWP_SHOWWINDOW|SWP_NOSIZE);
		m_cIconHappy.SetIcon(m_hIconSad);
		m_iHappyDelay = 0;
	}

}

BEGIN_MESSAGE_MAP(CMicroDlg, CDialog)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CMicroDlg message handlers

void CMicroDlg::OnCancel()
{
	// TODO: Add your specialized code here and/or call the base class

	//CDialog::OnCancel();
}

void CMicroDlg::OnOK()
{
	// TODO: Add your specialized code here and/or call the base class

	//CDialog::OnOK();
}

